package com.dats.db;

import com.dats.entity.BaseGroup;
import com.dats.entity.BaseGroup2;
import com.dats.entity.BaseObj;
import com.dats.hot.DManager;
import com.dats.pool.DatSPool;

import java.util.*;

/**
 * @author sig
 * @since 2016/9/25
 */
public class BaseDatS {

    public static <T extends BaseObj> void loadBase(String alias,
                                                    Class<T> clazz) {
        List<T> list = DatS.loadBaseList(alias, clazz);
        Map<Integer, T> map = new HashMap<>();

        for (T t : list) {

            if (null == t) continue;
            map.put(t.getId(), t);
        }
        DManager.initMg().calcMd5(clazz, list);
        DatSPool.baseMap.put(clazz, map);
    }

    public static <T extends BaseObj> T getBase(Class<T> clazz,
                                                int baseId) {
        Map<Integer, ?> map = DatSPool.baseMap.get(clazz);
        return null == map ? null : (T) map.get(baseId);
    }

    public static <T extends BaseObj> Map<Integer, T> getBaseMap(Class<T> clazz) {
        Map<Integer, ?> map = DatSPool.baseMap.get(clazz);
        return (Map<Integer, T>) map;
    }

    public static <T extends BaseObj> List<T> getBaseList(Class<T> clazz) {
        Map<Integer, ?> map = DatSPool.baseMap.get(clazz);
        return null == map ? null : new ArrayList<>((Collection<? extends T>) map.values());
    }

    public static List<Integer> getBaseIdList(Class<?> clazz) {
        Map<Integer, ?> map = DatSPool.baseMap.get(clazz);
        return null == map ? null : new ArrayList<>(map.keySet());
    }

    public static <T extends BaseGroup> List<T> getBaseGroup(Class<T> clazz,
                                                             int baseGroupId) {
        Map<Integer, List<?>> map = DatSPool.baseListMap.get(clazz);

        if (null == map) {

            if (null == DatSPool.baseListMap.get(clazz))
                initBaseGroup(clazz);
            map = DatSPool.baseListMap.get(clazz);
        }
        return null == map ? null : (List<T>) map.get(baseGroupId);
    }

    private static <T extends BaseGroup> void initBaseGroup(Class<T> clazz) {
        List<T> baseList = getBaseList(clazz);

        if (null == baseList) return;
        Map<Integer, List<?>> map = DatSPool.baseListMap.get(clazz);

        if (null == map) map = new HashMap<>();

        for (T tmp : baseList) {

            if (null == tmp) continue;
            List<T> list = (List<T>) map.get(tmp.getGroupId());

            if (null == list) list = new ArrayList<>();
            list.add(tmp);
            map.put(tmp.getGroupId(), list);
        }
        DatSPool.baseListMap.put(clazz, map);
    }

    public static <T extends BaseGroup2> List<T> getBaseGroup2(Class<T> clazz,
                                                               int groupId2) {
        Map<Integer, List<?>> map = DatSPool.baseListMap2.get(clazz);

        if (null == map) {

            if (null == DatSPool.baseListMap2.get(clazz))
                initBaseGroup2(clazz);
            map = DatSPool.baseListMap2.get(clazz);
        }
        return null == map ? null : (List<T>) map.get(groupId2);
    }

    private static <T extends BaseGroup2> void initBaseGroup2(Class<T> clazz) {
        List<T> baseList = getBaseList(clazz);

        if (null == baseList) return;
        Map<Integer, List<?>> map = DatSPool.baseListMap2.get(clazz);

        if (null == map) map = new HashMap<>();

        for (T tmp : baseList) {

            if (null == tmp) continue;
            List<T> list = (List<T>) map.get(tmp.getGroupId2());

            if (null == list) list = new ArrayList<>();
            list.add(tmp);
            map.put(tmp.getGroupId2(), list);
        }
        DatSPool.baseListMap2.put(clazz, map);
    }
}
